import operate from './operate.js'
import {getToken} from '@/common/auth.js';
import {config} from '@/static/config.js'

const http = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: operate.api() + options.url, //接口地址：前缀+方法中传入的地址https://abcde.jkqy120.com/api  http://localhost:5173
			method: options.method || 'GET', //请求方法：传入的方法或者默认是“GET”
			data: options.data || {}, //传递参数：传入的参数或者默认传递空集合
			header: {
				'Authorization': "Bearer " + getToken() //自定义请求头信息
			},
			success: (res) => {
				if (res.data?.status !== 400 && res?.statusCode !== 500) {
					const title = res.data?.message !== "" ? res.data?.message : JSON.stringify(
						res.data.data.errors)
					uni.showToast({
						title: title,
						icon: 'none',
						duration: 2000
					});
				}
				if (res?.statusCode === 500) {
					uni.showToast({
						title: "网络异常",
						icon: 'none',
						duration: 2000
					});
				}
				if (res.data?.status === 401) {
					uni.navigateTo({
						url: '/pages/login/index',
					})
				}
				resolve(res.data) //成功
			},
			// 这里的接口请求，如果出现问题就输出接口请求失败
			fail: (err) => {
				if(err?.errno==600001){
					uni.showToast({
							title: "网络异常,请检查是否连接网络！",
							icon: 'none',
							duration: 2000
						});
				}else{
				uni.showToast({
					title: "" + err.msg,
					icon: 'none'
				});	
				}
				reject(err)
			}
		})
	})
}
export default http